package Top;

public class test2_HeBingLianBiao {
    public static void main(String[] args) {
        ListNode L1 = new ListNode(1);
        L1.next = new ListNode(2);
        L1.next.next = new ListNode();

        ListNode L2 = new ListNode(1);
        L2.next = new ListNode(3);
        L2.next.next = new ListNode(4);
        L2.next.next.next = new ListNode();

        Solution6 solution6 = new Solution6();
        solution6.mergeTwoLists(L1, L2);
    }

}

class Solution6 {


    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        /*if (l1 == null) {
            return l2;
        }
        if (l2 == null) {
            return l1;
        }
        if (l1.val < l2.val) {
            System.out.println("l1的值小，摘出l1");
            System.out.println("l1.val = " + l1.val);
            l1.next = mergeTwoLists(l1.next, l2);
            System.out.println("l1.val = " + l1.val);
            return l1;
        } else {
            System.out.println("l2的值小，摘出l2");
            System.out.println("l2.val = " + l2.val);
            l2.next = mergeTwoLists(l1, l2.next);
            System.out.println("l2.val = " + l2.val);
            return l2;
        }*/
        ListNode preHead=new ListNode(-1);
        ListNode prev=preHead;
        while (l1!=null&&l2!=null){
            if (l1.val<l2.val){
                prev.next=l1;
                l1=l1.next;
            }else {
                prev.next=l2;
                l2=l2.next;
            }
            prev=prev.next;
        }
        prev.next=l1!=null?l1:l2;
        return preHead.next;

    }
}